#include "driver_xxx_read_test.h"

static xxx_handle_t gs_handle;        /**< xxx handle */

/**
 * @brief     read test
 * @param[in] times test times
 * @return    status code
 *            - 0 success
 *            - 1 read failed
 * @note      none
 */
uint8_t xxx_test(uint32_t times)
{
    uint8_t res;
    uint32_t i;
    uint32_t temperature_raw;
    uint32_t humidity_raw;
    float temperature;
    uint8_t humidity;
    xxx_info_t info;
   
    /* link interface function */
    DRIVER_XXX_LINK_INIT(&gs_handle, xxx_handle_t);
    DRIVER_XXX_LINK(&gs_handle, iic_init, xxx_interface_iic_init);
    DRIVER_XXX_LINK(&gs_handle, iic_deinit, xxx_interface_iic_deinit);
    DRIVER_XXX_LINK(&gs_handle, delay_ms, xxx_interface_delay_ms);
    DRIVER_XXX_LINK(&gs_handle, debug_print, xxx_interface_debug_print);

    /* get xxx information */
    res = xxx_info(&info);
    if (res != 0)
    {
        xxx_interface_debug_print("xxx: get info failed.\n");
       
        return 1;
    }
    else
    {
        /* print xxx information */
        xxx_interface_debug_print("xxx: chip is %s.\n", info.chip_name);
        xxx_interface_debug_print("xxx: manufacturer is %s.\n", info.manufacturer_name);
        xxx_interface_debug_print("xxx: interface is %s.\n", info.interface);
        xxx_interface_debug_print("xxx: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
        xxx_interface_debug_print("xxx: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
        xxx_interface_debug_print("xxx: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
        xxx_interface_debug_print("xxx: max current is %0.2fmA.\n", info.max_current_ma);
        xxx_interface_debug_print("xxx: max temperature is %0.1fC.\n", info.temperature_max);
        xxx_interface_debug_print("xxx: min temperature is %0.1fC.\n", info.temperature_min);
    }
    
    /* start basic read test */
    xxx_interface_debug_print("xxx: start read test.\n");
    
    /* xxx init */
    res = xxx_init(&gs_handle);
    if (res != 0)
    {
        xxx_interface_debug_print("xxx: init failed.\n");
       
        return 1;
    }
    
    /* delay 2000 ms for read */
    xxx_interface_delay_ms(2000);
    for (i = 0; i < times; i++)
    {
        /* read temperature and humidity */
        res = xxx_read_temperature_humidity(&gs_handle, (uint32_t *)&temperature_raw, (float *)&temperature, (uint32_t *)&humidity_raw, (uint8_t *)&humidity);
        if (res != 0)
        {
            xxx_interface_debug_print("xxx: read failed.\n");
            (void)xxx_deinit(&gs_handle);
           
            return 1;
        }
    
        /* print result */
        xxx_interface_debug_print("xxx[%u]: temperature: %.01fC.\n", i, temperature);
        xxx_interface_debug_print("xxx[%u]: humidity: %d%%.\n", i, humidity);

        /* delay 2000 ms*/
        xxx_interface_delay_ms(1000);
    }

    /* finish basic read test and exit */
    xxx_interface_debug_print("xxx: finish read test.\n");
    (void)xxx_deinit(&gs_handle);
    
    return 0;
}
